package com.cyf.db.mapper;

import com.cyf.db.bean.AppCar;
import com.cyf.db.bean.User;
import com.cyf.db.bean.AppUserCar;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * app用户与app注册车辆的关联表
 * Created by cage on 2017/2/16.
 */

public interface AppUserCarMapper {


    @Select("select b.* from app_usercar a, app_car b where a.carid = b.id and a.userid = #{userid}")
    List<AppCar> getCarByUserId(@Param("userid") Integer userId);

    @Select("select b.* from app_usercar a, app_car b where a.carid = b.id and a.userid = #{userid} and b.plate = #{plate}")
    AppCar getCarByUserIdAndPlate(@Param("userid") Integer userId,@Param("plate") String plate);

    @Select("select count(*) from app_usercar where userid = #{userid}")
    Integer getCountByUserId(@Param("userid") Integer userId);

    @Insert("insert into app_usercar(userid, carid, auth, sendMsg) values(#{userid}, #{carid}, 0, 0)")
    void insertUserCar(@Param("userid") Integer userId, @Param("carid") Integer carId);

    @Select("select b.* from app_usercar a, users b where a.userid = b.id and a.carid = #{carid}")
    List<User> getUserByCarId(@Param("carid") Integer carId);

    @Update("update app_usercar set auth = 1 where id = #{id}")
    void updateAuthById(@Param("id") Integer id);

    @Update("update app_usercar set auth = #{appUserCar.auth}, carcode = #{appUserCar.carcode}, cardrive = #{appUserCar.cardrive} where id = #{appUserCar.id}")
    void updateAuthInfo(@Param("appUserCar") AppUserCar appUserCar);

    @Select("select * from app_usercar where userid = #{userid} and carid = #{carid}")
    AppUserCar getByUserIdAndCarId(@Param("userid") Integer userid, @Param("carid") Integer carid);

    // 更新车辆认证图片的存储位置
    @Update("update app_usercar set filepath = #{filepath} where id = #{id}")
    void updateFilePath(@Param("filepath") String s, @Param("id") Integer id);

    // 删除app用户注册的某辆车
    @Delete("delete from app_usercar where userid = #{userid} and carid = #{carid}")
    void deleteByUserIdAndCarId(@Param("userid") Integer userid, @Param("carid") Integer carid);

    @Select("select a.* from app_usercar a, app_car b where a.carid = b.id and b.plate = #{plate}")
    List<AppUserCar> getByPlate(@Param("plate") String plate);

    @Select("select a.* from app_usercar a, app_car b " +
            "where a.carid = b.id and b.plate = #{plate} and a.userid != #{userid}")
    List<AppUserCar> getByPlateWithoutUserId(@Param("plate") String plate, @Param("userid") Integer userid);

    @Update("update app_usercar set sendMsg = 1 where id = #{id}")
    void updateSendMsgById(@Param("id") Integer id);

    @Update("update app_usercar set sendMsg = 0")
    void updateSendMsg();

    // 更新车的类型和品牌
    @Update("update carinfo set type = #{type}, brand = #{brand} where id = #{id}")
    void updateTypeAndBrandById(@Param("id") Integer id, @Param("type") String type, @Param("brand") String brand);

    @Delete("delete from app_usercar where userid = #{userid}")
    void deleteByUserId(@Param("userid") Integer userid);

    @Select("select au.mobile from app_car ac \n" +
            "LEFT JOIN app_usercar auc on auc.carid = ac.id\n" +
            "LEFT JOIN users au on au.id = auc.userid\n" +
            "where ac.plate = #{plate} limit 1")
    String selectByPlate(@Param("plate") String plate);
}
